<template>
	<!-- 税收完税证明页面 -->
	<view class="dqj">
		<view class="title">
			<text>税收完税证明</text>
			<text class="iconfont icon-sousuo" @click="searchPage"></text>
		</view>
		<view class="tabPage">
			<view :class="tabAction=='tabItem1'?'tabAction':''" @click="tabChange('item1')">
				<text>近一周</text>
				<!-- <uni-badge class="uni-badge-left-margin slTip" text="0"  /> -->
			</view>
			<view :class="tabAction=='tabItem2'?'tabAction':''" @click="tabChange('item2')">
				<text>近一个月</text>
			</view>
			<view :class="tabAction=='tabItem3'?'tabAction':''" @click="tabChange('item3')">
				<text>更多查询</text>
			</view>
		</view>
		<view class="cxjg">查询结果共有<text class="jgzs">{{tableData.length}}</text>条，当前为第<text
				class="dqws">{{tableData.length!==0?currentIndex+1:0}}</text>条，可横向滑动显示
		</view>
		<view class="uni-margin-wrap">
			<swiper class="swiper" @change="wszmqh" :current="currentIndex">
				<swiper-item v-for="(item,index) in tableData" :key="index">
					<view class="wsList">
						<uni-group class="dqjItem">
<!-- 							<view class="item">
								<view class="name">申报流水号：</view>
								<view class="con">{{item.sblsh}}</view>
							</view> -->
							<view class="item">
								<view class="name">纳税人识别号：</view>
								<view class="con">{{item.shxydm}}</view>
							</view>
							<view class="item">
								<view class="name">纳税人名称：</view>
								<view class="con">{{item.nsrmc}}</view>
							</view>
							<view class="item">
								<view class="name">房屋地址：</view>
								<view class="con">{{item.address}}</view>
							</view>
							<view class="item">
								<view class="name">租金所属期：</view>
								<view class="con">{{item.zjssq}}</view>
							</view>
							<view class="item">
								<view class="name">金额：</view>
								<view class="con">{{item.yjse}}元</view>
							</view>
							<view class="item">
								<view class="name">缴款日期：</view>
								<view class="con">{{item.kjrq}}</view>
							</view>
							<view class="btn">
								<button type="primary" size="mini" @click="kjfswszm(item)">邮件发送</button>
							<button type="primary" size="mini" @click="wszmtpyl(item)">预览图片</button>
							</view>
						</uni-group>
					</view>
				</swiper-item>
			</swiper> 
		</view>
		<view class="fy" v-show="tableData.length!==0">
			<uni-pagination :total="pageData.total" :pageSize="1" :current="currentIndex+1" @change="fyChange" title="标题文字"
				prev-text="前一页" next-text="后一页" /> 
		</view>
		<view class="maskbox" v-if="showMail"></view>
		<view class="dialog" v-if="showMail">
			<view class="dtitle">
				<text>信息</text>
			</view>
			<view class="dcon">
				<view class="dtip">税收完税证明开具成功，请填写邮箱地址进行发送</view>
				<view class="xzk">
					<combox-search label="收件人：" labelWidth="110rpx" emptyTips="无数据" :unikey="5"
						:candidates="candidates2" :isXzClear="true" placeholder="请输入邮箱地址" @getValue="getValue($event)"
						@input="jtValue($event)" :value="mail" :showMask="false"></combox-search>
					<button type="primary" size="mini" @click="addMail">添加</button>
				</view>
				<view class="zbMail">
					<view v-for="(item,index) in arrMail" :key="index" class="zbMailItem">
						<view class="yfsdMail">{{item}}</view>
						<icon type="clear" size="16" @click="cleanMail(item)" />
					</view>
				</view>
			</view>
			<view class="dbtn">
				<button type="primary" size="mini" @click="yjfs">确定</button>
				<button type="primary" size="mini" @click="closeMail">取消</button>
			</view>
		</view>
	</view>
</template>

<script>
	import comboxSearch from "@/components/cuihai-combox/cuihai-combox.vue"
	import {
		listSswszmByQO,
		sendWszmEmail,
		getNsrAndCzrByQO
	} from "@/utils/common-api.js"
	export default {
		data() {
			return {
				tabAction: "tabItem1",
				candidates2: [],
				ylMailData: [], //原本设置的邮箱地址
				mail: '',
				arrMail: [], //邮件数组
				showMail: false,
				tableData: [],
				formData: {
					wspzbh: "",
					address: "",
					kjrqq: "",
					kjrqz: "",
				},
				yjfsData: "", //邮件发送数据
				currentIndex: 0, //当前页
				pageData: {
					total: 0,
				}
			}
		},
		components: {
			comboxSearch,
		},
		onLoad(e) {
			// console.log(e)
			this.resetForm()
			// 首页进入
			if (Object.keys(e).length == 0) {
				var date = this.$commonCzwjs.date("","stamp");
				// 获取近一周起止时间
				let jyzDate = this.$commonCzwjs.getDateRange(date, 6, true)
				this.formData.kjrqq = jyzDate[0]
				this.formData.kjrqz = jyzDate[1]
				this.tabAction = "tabItem1"
				this.handleSearch()
				return
			} 
			// 更多查询
			if (e.type == "wszmcx") {
				this.tabAction = "tabItem3"
				let data = JSON.parse(e.wzmcxForm)
				this.formData = data
				this.handleSearch()
				return
			}
		},
		methods: {
			// 数据查询
			async handleSearch() {
				let res = await listSswszmByQO(this.formData)
				if (res.head.errorCode == "0") {
					this.tableData = res.body
					this.pageData.total = res.body.length
					this.currentIndex=0
				}else if(res.head.errorCode != "0"){
					uni.showToast({
						title: '数据请求失败',
						icon: 'error',
						duration: 3000
					})
				}
			},
			// 跳转查询页面
			searchPage() {
				this.$commonCzwjs.goPage({
					url: "/pages/sswszm/wszmcx"
				})
			},
			// 
			// 切换tab页 
			async tabChange(data) {
				if (data == 'item1'&&this.tabAction!="tabItem1") {
					this.resetForm()
					this.tabAction = "tabItem1"
					var date =this.$commonCzwjs.date("","stamp");
					// 获取近一周起止时间
					let jyzDate = this.$commonCzwjs.getDateRange(date, 6, true)
					this.formData.kjrqq = jyzDate[0]
					this.formData.kjrqz = jyzDate[1]
					this.handleSearch()
				}
				if (data == 'item2'&&this.tabAction!="tabItem2") {
					this.resetForm()
					this.tabAction = "tabItem2"
					var date =this.$commonCzwjs.date("","stamp");
					// 获取近一个月起止时间
					let jyzDate = this.$commonCzwjs.getDateRange(date, 30, true)
					this.formData.kjrqq = jyzDate[0]
					this.formData.kjrqz = jyzDate[1]
					this.handleSearch()
				}
				if (data == 'item3') {
					this.searchPage()
				}
			},
			// 显示邮件发送完税证明弹框
			async kjfswszm(val) {
				// 记录申报信息（发送邮件时使用）
				this.yjfsData = val
				// 获取出租人和承租人新增租赁时的邮件地址
				this.candidates2 = []
				this.ylMailData = []
				let res = await getNsrAndCzrByQO({
					sbId: this.yjfsData.id
				})
				if (res.head.errorCode == "0") {
					res.body.map(item => {
						this.ylMailData.push({
							nsrmc: item.nsrmc,
							yxdz: item.yxdz
						})
						this.candidates2.push(item.nsrmc + "(" + item.yxdz + ")")
					})
					this.showMail = true
				} else {
					this.showMail = true
				}

			},
			// 完税证明切换iconic 
			wszmqh(e) {
				// console.log(e.detail)
				this.currentIndex = e.detail.current 
			},
			// 分页切换
			fyChange(e) {
				this.currentIndex = e.current - 1
			},
			// 获取下来框选中邮件号码
			getValue(e) {
				// console.log(e)
				// 邮箱地址是否重复
				if (this.yjcfJy(e)) {
					return
				}
				this.arrMail.push(e)
			},
			// 输入添加邮件号码
			addMail() {
				// 邮箱地址本能为空
				if (!this.mail) {
					uni.showToast({
						title: '请输入邮箱地址',
						icon: 'error',
						duration: 1000
					});
					return
				}
				// 邮箱地址校验
				if (!this.$commonVaild.judgeYxdz(this.mail)) {
					uni.showToast({
						title: '邮箱地址错误',
						icon: 'error',
						duration: 1000
					});
					return
				}
				// 邮箱地址是否重复
				if (this.yjcfJy(this.mail)) {
					return
				}
				this.arrMail.push(this.mail)
				this.mail = ""
			},
			// 邮件重复校验
			yjcfJy(val) {
				// 邮箱地址是否重复
				let mailCf = false
				// console.log(this.arrMail)
				this.arrMail.map(item => {
					if (item == val) {
						mailCf = true
					}
				})
				if (mailCf) {
					uni.showToast({
						title: '邮箱地址已添加',
						icon: 'error',
						duration: 1000
					});
				}
				// console.log(mailCf)
				return mailCf
			},
			// 监听value值的改变
			jtValue(e) {
				this.mail = e
			},
			// 发送邮件信息弹框取消
			closeMail() {
				this.showMail = false
				this.mail = ""
				this.arrMail = []
			},
			// 删除要发送的邮件
			cleanMail(val) {
				this.arrMail = this.arrMail.filter(item => {
					return val != item
				})
			},
			//重置表单
			resetForm() {
				this.formData = {
					wspzbh: "",
					address: "",
					kjrqq: "",
					kjrqz: "",
				}
			},
			// 邮件发送
			async yjfs() {
				if (this.arrMail.length==0) {
					uni.showToast({
						title: '邮箱不能为空',
						icon: 'error',
						duration: 1000
					});
					return
				}
				let val = this.yjfsData
				// 邮箱过滤
				let arrMail = this.arrMail.map(item => {
					this.ylMailData.map(ylItem => {
						let zhMail = ylItem.nsrmc + "(" + ylItem.yxdz + ")"
						if (item == zhMail) {
							item = ylItem.yxdz
						}
					})
					return item
				})
				let params = {
					sbId: val.id,
					djxh: val.djxh,
					dzsphmArr: [val.dzsphm],
					yxdzList: arrMail
				}
				let res = await sendWszmEmail(params)
				if (res.head.errorCode == "0") {
					this.arrMail=[]
					this.showMail=false
					uni.showModal({
						title: '提示',
						content: '邮件发送成功',
						confirmColor: '#000000', //确定字体颜色
						showCancel: false, //没有取消按钮的弹框 
						buttonText: '确定',
						success: function(res) {}
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '邮件发送失败，请重新发送',
						confirmColor: '#000000', //确定字体颜色
						showCancel: false, //没有取消按钮的弹框 
						buttonText: '确定',
						success: function(res) {}
					});
				}
			},
			wszmtpyl(val){
				uni.navigateTo({
					url: "/pages/sswszm/pdfzimg?data=" + JSON.stringify(val)
				})
			}
		}
	}
</script>

<style>
	.title {
		width: 100%;
		height: 80rpx;
		background: #3171d5;
		text-align: center;
		line-height: 80rpx;
		color: white;
	}

	.title .iconfont {
		float: right;
		margin-right: 36rpx;
		font-size: 40rpx;
	}

	.tabPage {
		display: flex;
		height: 90rpx;
		font-size: 30rpx;
	}

	.tabPage>view {
		text-align: center;
		height: 90rpx;
		line-height: 90rpx;
		flex: 1;
		background: #007aff57;
		color: #ffffff;
	}

	.tabPage .slTip {
		position: relative;
		bottom: 20rpx;
	}

	.tabPage .tabAction {
		color: black;
		background: #ffffff;
		font-weight: bold;
	}

	.cxjg {
		padding: 20rpx 20rpx 0 20rpx;
		font-weight: bold;
		font-size: 26rpx;
		text-align: center;
	}

	.cxjg .jgzs {
		color: #3171d5;
	}

	.cxjg .dqws {
		color: red;
	}

	.wsList {
		padding: 0 20rpx;
		box-sizing: border-box;
		width: 100%;
		font-weight: bold;
		font-size: 26rpx;
		color: black;
		font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
	}

	.dqjItem {
		background: #ffffff;
		margin-top: 20rpx;
		border: 2rpx solid #e7e7e7;
	}

	::v-deep .uni-group .uni-group__content {
		padding: 0;
	}

	.uni-group__content {
		padding: 0;
	}

	.wsList .item {
		display: flex;
		padding: 20rpx 0;
		border-bottom: 2rpx solid #66666640;
	}

	.wsList .item:last-child {
		border-bottom: 0rpx solid #66666640;
	}

	.wsList .item .name {
		width: 200rpx;
		text-align: right;
		margin-right: 10rpx;
		padding-left: 20rpx;
	}

	.wsList .item .con {
		text-align: left;
	}

	.wsList .btn {
		text-align: center;
		padding: 20rpx;
	}

	.wsList .btn uni-button {
		margin: 0 10rpx;
		background-color: #3171d5;
		height: 70rpx;
		line-height: 70rpx;
	}
    .uni-margin-wrap{
		min-height: 700rpx; 
	}
	.swiper {
		min-height: 680rpx;
	}

	::v-deep uni-swiper .uni-swiper-dots-horizontal {
		white-space: pre-wrap !important;
		width: 90%;
	}

	::v-deep uni-swiper .uni-swiper-dots-horizontal .uni-swiper-dot {
		margin-top: 10rpx;
	}

	.dialog {
		width: 95%;
		min-height: 750rpx;
		position: fixed;
		top: 30%;
		left: 50%;
		transform: translate(-50%, -30%);
		border: 2rpx solid black;
		background: #ffffff;
		z-index: 100;
	}

	.dialog .dtitle {
		width: 100%;
		padding: 10rpx;
		box-sizing: border-box;
		color: #ffffff;
		background: #3171d5;
	}

	.dialog .dtip {
		margin-top: 20rpx;
		font-weight: bold;
		font-size: 26rpx;
		text-align: center;
	}

	.xzk {
		padding: 10rpx;
		display: flex;
		align-items: center;
	}

	.xzk uni-button {}

	.xzk ::v-deep .uni-combox__label {
		text-align: right;
		font-weight: bold;
		font-size: 26rpx;
		color: black;
	}

	.xzk ::v-deep .combox_input_view {
		width: 400rpx;
	}

	.zbMail {
		padding: 0 20rpx;
		max-height: 340rpx;
		overflow: scroll
	}

	.yfsdMail {
		padding: 10rpx 0;
		word-break: break-word; //文本超出 自动换行
	}

	.zbMailItem {
		display: flex;
		align-items: center;
	}

	.zbMailItem ::v-deep .uni-icon-clear {
		margin-left: 10rpx;
	}

	.dialog .dbtn {
		width: 100%;
		text-align: center;
		position: absolute;
		bottom: 20rpx;
	}

	.dialog .dbtn uni-button {
		margin: 0 10rpx;
	}

	.maskbox {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		height: 100vh;
		z-index: 99;
		background-color: rgba(0, 0, 0, 0.4);
	}

	.fy {
		margin: 0 20rpx 0 20rpx;

	}

	::v-deep .uni-pagination__btn {
		background: #00000063;
		opacit: 1;
	}

	::v-deep .uni-pagination__child-btn {
		color: #ffffff;
	}
</style>